//
//  STRightViewController.swift
//  STMusic
//
//  Created by stoneobs on 16/10/18.
//  Copyright © 2016年 stoneobs. All rights reserved.
//  这将会是一个播放界面

import UIKit

class YYMusciPlayerController: UIViewController {


    var titleLable:STLabel?//title
    
    
    var backGroundImageView:UIImageView?
    
    var bannerImageView:UIImageView?
    
    var playStyleImageView:UIImageView?//播放模式view 循环，单曲，随机
    
    var slider:UISlider?
    
    var leftTimeLable:STLabel?
    
    var rightTimeLable:STLabel?
    
    
    var datasouce = [UIImage.init(named: "1"),
                     UIImage.init(named: "2"),
                     UIImage.init(named: "3"),
                     UIImage.init(named: "4"),
                     UIImage.init(named: "5"),
                     UIImage.init(named: "6"),
                     UIImage.init(named: "7"),
                     UIImage.init(named: "8"),
                     UIImage.init(named: "9"),
                     UIImage.init(named: "10")]
    
    static let shardPlayer = YYMusciPlayerController.init();
    
    private init() {
        super.init(nibName: nil, bundle: nil);
    }

    private override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
       
    }
    internal required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.init(gradientStyle: .topToBottom, withFrame: self.view.bounds, andColors: [FlatWhiteDark(),FlatWhite()]);
        self.configSubviews();
        // Do any additional setup after loading the view.
    }
    func configSubviews()  {
        weak var weakSelf = self;


        
        
        self.backGroundImageView = UIImageView.init(frame: .init(x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_HEIGHT));
        let visEffect = UIBlurEffect.init(style: .light);
        let visoulView = UIVisualEffectView.init(frame: (backGroundImageView?.bounds)!)
        visoulView.effect = visEffect;
        visoulView.alpha = 1;
        backGroundImageView?.addSubview(visoulView);
        self.backGroundImageView?.contentMode = .scaleAspectFill;
        self.backGroundImageView?.clipsToBounds = true;
        self.backGroundImageView?.image = UIImage.init(named: "6");
        self.view.addSubview(self.backGroundImageView!);
        
        
        self.bannerImageView = UIImageView.init(frame: .init(x: 0, y: 100, width: SCREEN_WIDTH * 0.8, height: SCREEN_WIDTH * 0.8));
        self.bannerImageView?.centerX = SCREEN_WIDTH / 2;
        self.bannerImageView?.layer.cornerRadius = 4;
        self.bannerImageView?.clipsToBounds = true;
        self.bannerImageView?.image = UIImage.init(named: "6");
        self.bannerImageView?.contentMode = .scaleAspectFill;
        self.bannerImageView?.clipsToBounds = true;
        self.view.addSubview(self.bannerImageView!);
        
        //底部按钮
        let bootomCenterY = SCREEN_HEIGHT - 64 - 20;
        self.playStyleImageView = UIImageView.init(frame: .init(x: 0, y: 0, width: 20, height: 17));
        self.playStyleImageView?.image = UIImage.init(named: "loop_all_icon");
        self.playStyleImageView?.centerY = bootomCenterY;
        self.playStyleImageView?.right = SCREEN_WIDTH - 30;
        self.playStyleImageView?.addGestureRecognizer(UITapGestureRecognizer.init(target: self, action: #selector(playStyleAction)))
        self.playStyleImageView?.isUserInteractionEnabled = true;
        self.view.addSubview(self.playStyleImageView!);
        

        let playButton:STButton = STButton.init(frame: .init(x: 0, y: 0, width: 60, height: 60), title: nil, titleColor: nil, titleFont: 0, cornerRadius: 30, backgroundColor: .clear, backgroundImage: UIImage.init(named: "big_play_button"), image: nil);
        playButton.setBackgroundImage(UIImage.init(named: "big_play_button"), for: .normal);
        playButton.setBackgroundImage(UIImage.init(named: "big_pause_button"), for: .selected);
        playButton.centerX = self.view.centerX;
        playButton.centerY =  bootomCenterY;
        playButton.clicAction = {but in
            weakSelf?.playButtonAction(but: but!);
        }
        self.view.addSubview(playButton);
   
        

        let lastButton:STButton = STButton.init(frame: .init(x: 0, y: 0, width: 20, height: 28), title: nil, titleColor: nil, titleFont: 0, cornerRadius: 0, backgroundColor: .clear, backgroundImage: UIImage.init(named: "prev_song"), image: nil);
        lastButton.setBackgroundImage(UIImage.init(named: "prev_song"), for: .normal);
        lastButton.setBackgroundImage(UIImage.init(named: "prev_song"), for: .selected);
        lastButton.centerX = self.view.centerX - 80;
        lastButton.centerY =  bootomCenterY;
        lastButton.clicAction = {but in
            weakSelf?.lastButtonAction(but: but!);
        }
        lastButton.contentHorizontalAlignment = .center;
        self.view.addSubview(lastButton);
    

        
        let nextButton:STButton = STButton.init(frame: .init(x: 0, y: 0, width: 20, height: 28), title: nil, titleColor: nil, titleFont: 0, cornerRadius: 0, backgroundColor: .clear, backgroundImage: UIImage.init(named: "next_song"), image: nil);
        nextButton.setBackgroundImage(UIImage.init(named: "next_song"), for: .normal);
        nextButton.setBackgroundImage(UIImage.init(named: "next_song"), for: .selected);
        nextButton.centerX = self.view.centerX + 80;
        nextButton.centerY =  bootomCenterY;
        nextButton.clicAction = {but in
            weakSelf?.nextButtonAction(but: but!);
        }
        nextButton.contentHorizontalAlignment = .center;
        self.view.addSubview(nextButton);
  
        
        let menuButton:STButton = STButton.init(frame: .init(x: 0, y: 0, width: 20, height: 18), title: nil, titleColor: nil, titleFont: 0, cornerRadius: 0, backgroundColor: .clear, backgroundImage: UIImage.init(named: "menu"), image: nil);
        menuButton.setBackgroundImage(UIImage.init(named: "menu"), for: .normal);
        menuButton.setBackgroundImage(UIImage.init(named: "menu"), for: .selected);
         menuButton.setBackgroundImage(UIImage.init(named: "menu"), for: .highlighted);
        menuButton.left = 30;
        menuButton.centerY =  bootomCenterY;
        menuButton.clicAction = {but in
            weakSelf?.menuButtonAction(but: but!);
        }
        menuButton.contentHorizontalAlignment = .center;
        self.view.addSubview(menuButton);
     
        //头部
        let retunButton:STButton = STButton.init(frame: .init(x: 20, y: 0, width: 25, height: 25), title: nil, titleColor: nil, titleFont: 0, cornerRadius: 0, backgroundColor: .clear, backgroundImage: UIImage.init(named: "pinkRetun"), image: nil);
        retunButton.setBackgroundImage(UIImage.init(named: "pinkRetun"), for: .normal);
        retunButton.setBackgroundImage(UIImage.init(named: "pinkRetun"), for: .selected);
        retunButton.centerY =  44;
        retunButton.clicAction = {but in
            weakSelf?.backAction();
        }
        retunButton.contentHorizontalAlignment = .center;
        self.view.addSubview(retunButton);
        
        let likeButton:STButton = STButton.init(frame: .init(x:SCREEN_WIDTH - 20 - 25, y: 0, width: 23, height: 21.5), title: nil, titleColor: nil, titleFont: 0, cornerRadius: 0, backgroundColor: .clear, backgroundImage: UIImage.init(named: "pinkRetun"), image: nil);
        likeButton.setBackgroundImage(UIImage.init(named: "empty_heart"), for: .normal);
        likeButton.setBackgroundImage(UIImage.init(named: "red_heart"), for: .selected);
        likeButton.setBackgroundImage(likeButton.currentBackgroundImage, for: .highlighted);
        likeButton.centerY =  44;
        likeButton.clicAction = {but in
            weakSelf?.likeButtonAction(but: but!);
        }
        likeButton.contentHorizontalAlignment = .center;
        self.view.addSubview(likeButton);
        
        self.titleLable = STLabel.init(frame: .init(x: 60, y: 0, width: SCREEN_WIDTH - 120  , height: 16), text: "晴天", textColor: .white, font: 16, isSizetoFit: false, textAlignment: .center);
        self.titleLable?.centerY = 44;
//        self.titleLable?.backgroundColor = YYThemeStyleManger.defultManger.gradualColor(frame: (self.titleLable?.bounds)!);
        self.view.addSubview(self.titleLable!);
        
        //滑竿
        self.slider = UISlider.init(frame: .init(x: 80, y: 0, width: SCREEN_WIDTH - 160, height: 20));
        self.slider?.backgroundColor = .clear;
        self.slider?.setThumbImage(UIImage.init(named: "music_slider_circle"), for: .normal);
        self.slider?.bottom = playButton.top - 40;
        self.view.addSubview(self.slider!);
        
        self.leftTimeLable = STLabel.init(frame: .init(x: 0, y: 0, width: 75, height: 12), text: "0:12", textColor: .white, font: 12, isSizetoFit: false, textAlignment: .center);
        self.leftTimeLable?.centerY = (self.slider?.centerY)!;
        self.view.addSubview(self.leftTimeLable!);
        
        self.rightTimeLable = STLabel.init(frame: .init(x: SCREEN_WIDTH - 75, y: 0, width: 75, height: 12), text: "2:12", textColor: .white, font: 12, isSizetoFit: false, textAlignment: .center);
        self.rightTimeLable?.centerY = (self.slider?.centerY)!;
        self.view.addSubview(self.rightTimeLable!);
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
        // MARK:Action Method
    func backAction()  {
        STLayoutViewController.defaultLayout().dismissCurrentController(true);
    }

    func playStyleAction()  {
        
    }
    func playButtonAction(but:UIButton)  {
        self.testAnimation();
    }
    func lastButtonAction(but:UIButton)  {
         self.testAnimation();

    }
    func nextButtonAction(but:UIButton)  {
         self.testAnimation();
    }
    func menuButtonAction(but:UIButton)  {
        
    }

    func likeButtonAction(but:UIButton)  {
        
    }

    func testAnimation()  {
        let index = Int(arc4random_uniform(UInt32(self.datasouce.count - 1)))
        let image = self.datasouce[index];
        
        self.bannerImageView?.image = image;
        //self.bannerImageView?.showTransAnimation(type: .STrippleEffect);
        
        self.backGroundImageView?.image = image;
        //self.backGroundImageView?.showTransAnimation(type: .STkCATransitionFade);
        self.view.showTransAnimation(type: .STkCATransitionFade);
    }
}
